package com.nf.health.app.widget.custom_chatrow;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.hyphenate.exceptions.HyphenateException;
import com.nf.health.app.R;
import com.nf.health.app.intervention_details.activity.HealthSurveyActivity;
import com.nf.health.app.utils.ActivityUtils;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by 恩福 on 2016/10/10.
 * 医生问卷 Item
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class EaseChatRowDoctorQuestion extends EaseChatRow {

    private TextView tvTitle;
    private TextView tvContent;
    private String messageid;

    public EaseChatRowDoctorQuestion(Context context, EMMessage message, int position, BaseAdapter adapter,int state) {
        super(context, message, position, adapter,state);
    }

    @Override
    protected void onInflatView() {
        if(message.direct() == EMMessage.Direct.RECEIVE){
            inflater.inflate(R.layout.ease_row_received_doctor_question,this);
        }
    }

    @Override
    protected void onFindViewById() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvContent = (TextView) findViewById(R.id.tv_content);
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onSetUpView() {

//        Glide.with(context).load(R.drawable.ic_msg_question).into(userAvatarView);

        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        try {
            JSONObject object = new JSONObject("{"+txtBody.getMessage()+"}");
            String title = (String) object.get("title");
            String content = (String) object.get("content");
            messageid = (String) object.get("messageid");
            tvTitle.setText(title);
            tvContent.setText(content);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(!message.isAcked() && message.getChatType() == EMMessage.ChatType.Chat){
            try {
                EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
            } catch (HyphenateException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    protected void onBubbleClick() {
        if(!TextUtils.isEmpty(messageid)){
            Bundle bundle = new Bundle();
            bundle.putString(HealthSurveyActivity.EXTRA_MSG_ID, messageid);
            ActivityUtils.showActivity(context, HealthSurveyActivity.class, bundle);
        }
    }
}
